package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeHidden;
import com.ruoyi.food.mapper.OpmFoodSafeHiddenMapper;
import com.ruoyi.food.service.IOpmFoodSafeHiddenService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品质量安全隐患Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeHiddenServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeHiddenService {
    @Autowired
    private OpmFoodSafeHiddenMapper opmFoodSafeHiddenMapper;

    /**
     * 查询食品质量安全隐患
     *
     * @param id 食品质量安全隐患ID
     * @return 食品质量安全隐患
     */
    @Override
    public OpmFoodSafeHidden selectOpmFoodSafeHiddenById(Long id) {
        OpmFoodSafeHidden result = opmFoodSafeHiddenMapper.selectOpmFoodSafeHiddenById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询食品质量安全隐患列表
     *
     * @param opmFoodSafeHidden 食品质量安全隐患
     * @return 食品质量安全隐患
     */
    @Override
    public List<OpmFoodSafeHidden> selectOpmFoodSafeHiddenList(OpmFoodSafeHidden opmFoodSafeHidden) {
        return opmFoodSafeHiddenMapper.selectOpmFoodSafeHiddenList(opmFoodSafeHidden);
    }

    /**
     * 新增食品质量安全隐患
     *
     * @param opmFoodSafeHidden 食品质量安全隐患
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeHidden(OpmFoodSafeHidden opmFoodSafeHidden) {
        opmFoodSafeHidden.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeHidden.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeHidden.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeHidden.getFileIdList(), opmFoodSafeHidden.getId().toString());
        return opmFoodSafeHiddenMapper.insertOpmFoodSafeHidden(opmFoodSafeHidden);
    }

    /**
     * 修改食品质量安全隐患
     *
     * @param opmFoodSafeHidden 食品质量安全隐患
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeHidden(OpmFoodSafeHidden opmFoodSafeHidden) {
        opmFoodSafeHidden.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeHidden.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeHidden.getFileIdList(), opmFoodSafeHidden.getId().toString());
        return opmFoodSafeHiddenMapper.updateOpmFoodSafeHidden(opmFoodSafeHidden);
    }

    /**
     * 批量删除食品质量安全隐患
     *
     * @param ids 需要删除的食品质量安全隐患ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeHiddenByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeHiddenMapper.deleteOpmFoodSafeHiddenByIds(ids);
    }

    /**
     * 删除食品质量安全隐患信息
     *
     * @param id 食品质量安全隐患ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeHiddenById(Long id) {
        return opmFoodSafeHiddenMapper.deleteOpmFoodSafeHiddenById(id);
    }
}
